package weaver.interfaces.hoda.duss.action;

import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022/03/08
 * @Description: 工程采购/施工合同变更审批流程
 * @ModifyLog:
 **/
/*
 *@Description: 加班申请流程修改加班最终数据表的加班时长
 *@Param:
 *@return:
 *@Author: Dusongsong
 *@Date: 2022/03/08
 */
public class WeaOvertimeApplicatAction implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        baseBean.writeLog( "-----WeaOvertimeApplicatAction-----"+requestid );
        rs.executeQuery("select id from "+tablename+" where requestid=?",requestid);
        rs.next();
        int mainid = Util.getIntValue(rs.getString( "id" ),-1);
        try {
            rs.executeQuery( "select xm,jbrq,jbjsrq,jbxs*60 as jbxs from "+tablename+"_dt1 where mainid=?",mainid );
            while (rs.next()){
                String xm = Util.null2String(rs.getString( "xm" ));
                int jbxs = Util.getIntValue(rs.getString( "jbxs" ));
                String jbrq = Util.null2String(rs.getString( "jbrq" ));
                String jbjsrq = Util.null2String(rs.getString( "jbjsrq" ));
                String sql = "select requestid,resourceid,fromdatedb,todatedb,durationrule," +
                        "sum(cast(duration_min as NUMERIC)) duration_min  from kq_flow_overtime " +
                        "where computingmode=1 and requestid='"+requestid+"' and resourceid='"+xm+"'" +
                        " and fromdatedb='"+jbrq+"' and todatedb='"+jbjsrq+"'" +
                        "group by requestid,resourceid,fromdatedb,todatedb,durationrule";
                rs1.executeQuery(sql);
                rs1.next();
                int duration_min = Util.getIntValue(rs1.getString( "duration_min" ));
                baseBean.writeLog("duration_min"+duration_min+",jbxs="+jbxs+",jbxs="+jbxs);
                if(duration_min < jbxs ){
                    int newmin = jbxs-duration_min;
                    baseBean.writeLog("newmin111"+newmin);
                    String update_sql = "update kq_flow_overtime set duration_min=(duration_min+"+newmin+") " +
                            "where id=(select min(id) from kq_flow_overtime " +
                            "where requestid=? and resourceid=? and fromdatedb=? and todatedb=?)";
                    rs.executeUpdate( update_sql,new Object[]{requestid,xm,jbrq,jbjsrq} );
                }
                if(duration_min > jbxs ){
                    int newmin = duration_min-jbxs;
                    baseBean.writeLog("newmin222"+newmin);
                    String update_sql = "update kq_flow_overtime set duration_min=(duration_min-"+newmin+") " +
                            "where id=(select min(id) from kq_flow_overtime " +
                            "where requestid=? and resourceid=? and fromdatedb=? and todatedb=?)";
                    rs.executeUpdate( update_sql,new Object[]{requestid,xm,jbrq,jbjsrq} );
                }

            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            baseBean.writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
